package com.leo.client.topics;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeoutException;

/**
 *
 * @author xuexiaolei
 * @version 2017年08月20日
 */
public class EmitLogDirect {
    private static final String EXCHANGE_NAME = "topic_logs";
    private static final List<String> SEVERITIES = new ArrayList<>();
    static {
        // 符号“#”匹配一个或多个词，符号“*”匹配不多不少一个词
        SEVERITIES.add("*.*.rabbit");
        SEVERITIES.add("a.b.rabbit");
        SEVERITIES.add("c.rabbit");//丢失，因为不匹配
        SEVERITIES.add("lazy.#");
        SEVERITIES.add("lazy.a.b");
        SEVERITIES.add("lazy.c");
        SEVERITIES.add("*.orange.*");
        SEVERITIES.add("a.orange.b");
        SEVERITIES.add("c.orange");//丢失，因为不匹配
    }

    public static void main(String[] argv) throws java.io.IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        for (int i = 0; i < 30; i++) {
            String severity = SEVERITIES.get(new Random().nextInt(SEVERITIES.size()));//随机产生一个routingKey
            String message = "some logs " + i;
            channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes());
            System.out.println(" [x] Sent '" + severity + "':'" + message + "'");
        }

        channel.close();
        connection.close();
    }
}
